<?php
/**
 * Created by PhpStorm.
 * User: FDG367
 * Date: 2017/9/20
 * Time: 14:30
 */
namespace common\helps;

use common\helps\soap\SoapClient;
use common\models\CustMaster;
use common\models\LogWebException;
use yii;

class Soap extends CustMaster{
	const API_SUCCESS	= 1;
	/**
	 * @inheritdoc
	 */
	public function rules()
	{
		return [
			[['mobile', 'pwd', 'wx_openid', 'wx_unionid', 'wx_nickname', 'wx_sex', 'x_province', 'wx_city', 'wx_country', 'wx_headimgurl', 'wx_privilege', 'memo', 'use_flg', 'email', 'creator', 'create_time', 'editor', 'edit_time', 'username', 'member_no','sex', 'member_source', 'consum_amount', 'current_points ', 'birthday', 'province_code', 'province_name', 'city_code', 'city_name', 'district_code', 'district_name', 'adddress' ], 'safe'],
			['mobile', 'trim'],
			//['mobile', 'trim'],正则手机号 TODO
		];
	}

	/**
	 * @param 获取线下会员信息
	 * @param memberNo 获取线下会员信息
	 * @param mobileNo 手机号
	 * @param weixinId 微信号
	 * @return bool|mixed
	 */
	public function getMembers(){
		error_log('info-> getMember start');
		//获取会员信息参数
		$params = [
			'memberNo'	=>$this->member_no 	? $this->member_no	:'',
			'mobileNo'	=>$this->mobile		? $this->mobile		:'',
			'weixinId'	=>$this->wx_openid	? $this->wx_openid	:'',
		];

		try{
			$model	= SoapClient::soapClient('getMembers',$params);
			if(!$model){
				error_log('error->message:接口异常；info-> params'.json_encode($params));
				throw new \Exception('接口请求错误！');
			}

			//会员数据
			$getMembersResult =json_decode($model->GetMembersResult);

			if(!isset($getMembersResult->result)|| self::API_SUCCESS != $getMembersResult->result){
				error_log('error->message:请求数据错误；info-> params'.json_encode($params));
				throw new \Exception('请求数据错误！');
			}
			error_log('info-> getMembers end');
			return $getMembersResult;
		}catch (\Exception $e){
			error_log('error-> getMember 异常: ' . $e->getMessage());
			return false;
			//记录接口异常
		}

	}

	/**
	 * @param 注册线下会员
	 */

	public function register(){
		error_log('info-> register start');
		$params = [
			//mod by FDG367  2018-04-10 574 注册线下会员时，没有对应的会员名称，统一为“网上会员” start
			'name'			=>$this->username?$this->username:GENERN_NAME,
			//mod by FDG367  2018-04-10 574 注册线下会员时，没有对应的会员名称，统一为“网上会员” end
			'sex'			=>($this->sex == 2)?'男':'女',
			'mobile'		=>$this->mobile,

			//mod by FDG367 20180115 个人中心/出生日期默认时间修改 448 start
			'birthday'		=>$this->birthday ? $this->birthday :'1980-1-1',
			//mod by FDG367 20180115 个人中心/出生日期默认时间修改 448 end
			'province'		=>$this->province_name ? $this->province_name :'',
			'city'			=>$this->city_name ? $this->city_name :'',
			'district'		=>$this->district_name ? $this->district_name :'',
			'adddress'		=>$this->adddress ? $this->adddress :'',
			'email'			=>$this->email 	  ? $this->email :'',
			'weixinId'		=>$this->wx_openid ? $this->wx_openid :'',
		];

		try{
			$module	= SoapClient::soapClient('Register',$params);
			if(!$module){
				error_log('error->message:接口异常；info-> params'.json_encode($params));
				throw new \Exception('接口异常！');
			}

			//会员数据
			$registerResult =json_decode($module->RegisterResult);

			if(!isset($registerResult->result) || self::API_SUCCESS != $registerResult->result){
				error_log('info->message:请求数据错误；info-> params'.json_encode($params).'info->result:'.json_encode( $registerResult));
				throw new \Exception('请求数据错误！');
			}
			error_log('info-> register end');
			return $registerResult;
		}catch (\Exception $e){
			error_log('error-> register 异常: ' . $e->getMessage());
			return false;
		}



	}

	/**
	 * @param 注册线下会员
	 */

	public function modify(){
		error_log('info-> modify start');
		$params = [
			'memberNo'		=>$this->member_no,
			'name'			=>$this->username,
			'sex'			=>(CustMaster::USER_SEX_MAN==$this->sex)?'男':'女',
			'birthday'		=>date('Y-m-d\TH:i:s',strtotime($this->birthday)),//date('Y-m-d ',strtotime($this->birthday)),
			'province'		=>$this->province_name,
			'city'			=>$this->city_name,
			'district'		=>$this->district_name,
			'adddress'		=>$this->adddress,
			'email'			=>$this->email,
		];


		try{

			$module	= SoapClient::soapClient('Modify',$params);

			if(!$module){

				error_log('info->message:接口异常；info-> params'.json_encode($params));
				$log	= new LogWebException();
				throw new \Exception('接口请求错误！');
			}

			//会员数据
			$modifyResult =json_decode($module->ModifyResult);

			if(!isset($modifyResult->result)|| self::API_SUCCESS != $modifyResult->result){
				error_log('info->message:请求数据错误；info-> params'.json_encode($params).'info->result:'.json_encode($modifyResult));
				$log	= new LogWebException();
				$log->saveLog(
					WEB_EXCEPTION_NUM_1022,
					'info->message:接口异常；info-> params'.json_encode($params),
					$modifyResult,
					2
				);
				throw new \Exception('请求数据错误！');
			}
			error_log('info-> modify end');
			return $modifyResult;
		}catch (\Exception $e){
			error_log('error-> modify 异常: ' . $e->getMessage());
			return false;
		}
	}

	/**
	 * @param 解绑微信
	 */
	public function unbindWeiXinId(){
		error_log('info-> unbindWeiXinId start');
		$params = [
			'weixinId'		=>$this->wx_openid,
		];

		try{
			$module	= SoapClient::soapClient('unbindWeiXinId',$params);
			if(!$module){
				error_log('info->message:接口异常；info-> params'.json_encode($params));
				throw new \Exception('接口请求错误！');
			}

			//会员数据
			$Result =json_decode($module->UnbindWeiXinIdResult);

			if(!isset($Result->result)|| self::API_SUCCESS != $Result->result){
				error_log('info->message:请求数据错误；info-> params'.json_encode($params).'info->result:'.json_encode($Result));
				throw new \Exception('请求数据错误！');
			}
			error_log('info-> unbindWeiXinId end');
			return $Result;
		}catch (\Exception $e){
			error_log('error-> unbindWeiXinId 异常: ' . $e->getMessage());
			return false;
		}

	}

	/**
	 * @param 绑定微信
	 */
	public function bindWeiXinId(){
		error_log('info-> bindWeiXinId start');
		$params = [
			'memberNo'		=>$this->member_no,
			'weixinId'		=>$this->wx_openid
		];

		try{
			$module	= SoapClient::soapClient('BindWeiXinId',$params);
			if(!$module){
				error_log('info->message:接口异常；info-> params'.json_encode($params));
				throw new \Exception('接口请求错误！');
			}

			//会员数据
			$Result =json_decode($module->BindWeiXinIdResult);

			if(!isset($Result->result) || self::API_SUCCESS != $Result->result){
				error_log('info->message:请求数据错误；info-> params'.json_encode($params).'info->result:'.json_encode($Result));
				throw new \Exception('请求数据错误！');
			}
			error_log('info-> bindWeiXinId end');
			return $Result;
		}catch (\Exception $e){
			error_log('error-> bindWeiXinId 异常: ' . $e->getMessage());
			return false; 
		}
	}
}